package com.sunye.rabbit_mq_delay;


import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbitDelayController")
public class RabbitDelayController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //参考 https://www.freesion.com/article/8084669528/ 第六点 利用RabbitMQ插件实现延迟队列
    //http://localhost:8088/rabbitDelayController/delayMsg?msg=1&seconds=1
    @RequestMapping("delayMsg")
    public void delayMsg(String msg, Integer seconds) {
        sendDelayMsg(msg, seconds);
    }


    public void sendDelayMsg(String msg, Integer seconds) {
        //msg中可以放业务id
        rabbitTemplate.convertAndSend(MQCont.EXCHANGE_DIRECT_ORDER_CANCEL, MQCont.ROUTINGKEY_ORDER_CANCEL, msg, message ->{
            //MessageProperties 封装消息的一些属性，属性比较多，我这里只设置以下消息的持久化，PERSISTENT-持久化  NON_PERSISTENT-非持久化
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setDelay(seconds*60*1000);
            return message;
        });
    }






}
